package io.renren.controller;

import io.renren.constant.AppConstant;
import io.renren.entity.AppUpdateEntity;
import io.renren.service.AppUpdateService;
import io.renren.vo.RespModel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/app/version")
@Api(tags = "版本检测更新")
@Component
public class AppVersionController {

    private static final Logger logger = LoggerFactory.getLogger(AppVersionController.class);

    @Autowired
    private AppUpdateService appUpdateService;

    /**
     * 获取版本号
     *
     * @param applicationId applicationId
     * @return 版本信息
     */
    @GetMapping(value = "/version", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "查询版本号", produces = MediaType.APPLICATION_JSON_VALUE)
//    @com.cxytiandi.encrypt.springboot.annotation.Decrypt
    @com.cxytiandi.encrypt.springboot.annotation.Encrypt
    public RespModel version(@RequestParam("applicationId") String applicationId) {
        Map<String, Object> param = new HashMap<>();
        param.put("appid", applicationId);
        List<AppUpdateEntity> list = appUpdateService.queryListByParam(param);
        if (list == null || list.size() == 0) {
            return new RespModel().getRespModelInstance(AppConstant.STATU_SUCCESS,"success",null);
        }

        //主版本号：重构或者低级版本积累升级
        Integer mainno = 0;
        //次版本号：添加/修改重大模块以及比低版本积累升级
        Integer minorno = 0;
        //修订版本号：小的调整以及修复bug升级的版本号
        Integer reviseno;

        AppUpdateEntity last;

        List<AppUpdateEntity> filterList = new ArrayList<>();

        // 主版本
        for (AppUpdateEntity entity : list) {
            mainno = entity.getMainno() > mainno ? entity.getMainno() : mainno;
        }
        for (AppUpdateEntity entity : list) {
            if (entity.getMainno().equals(mainno)) {
                filterList.add(entity);
            }
        }
        list = new ArrayList<>(filterList);
        filterList.clear();
        // 中版本
        for (AppUpdateEntity entity : list) {
            minorno = entity.getMinorno() > minorno ? entity.getMinorno() : minorno;
        }
        for (AppUpdateEntity entity : list) {
            if (entity.getMinorno().equals(minorno)) {
                filterList.add(entity);
            }
        }
        list = new ArrayList<>(filterList);
        filterList.clear();
        // 小版本
        last = list.get(0);
        reviseno = last.getReviseno();
        for (AppUpdateEntity entity : list) {
            if (entity.getReviseno() > reviseno) {
                reviseno = entity.getReviseno();
                last = entity;
            }
        }

        return new RespModel().getRespModelInstance(AppConstant.STATU_SUCCESS,"success",last);
    }

}
